//
//  RandomHelper.m
//  PiggyPicnic
//
//  Created by Hunk on 10-7-19.
//  Copyright 2010 Vanceinfo. All rights reserved.
//

#import "RandomHelper.h"


@implementation RandomHelper

+(int) getRandomNum:(int) low to:(int) high
{
	srand((unsigned int)(time(0)));
	
	int result = low + (arc4random() % (high - low + 1));
	return result;
}

+(NSArray*)getRandomArray:(int)minNumber max:(int)maxNumber
{	
	int n1st = [RandomHelper getRandomNum:minNumber to:maxNumber];
	int n2nd;
	int n3rd;
	int n4th;
	int n5th;
	int nTemp2nd = [RandomHelper getRandomNum:minNumber to:maxNumber];
	if(nTemp2nd != n1st)
	{
		n2nd = nTemp2nd;
	}
	while(nTemp2nd == n1st)
	{
		nTemp2nd = [RandomHelper getRandomNum:minNumber to:maxNumber];
		n2nd = nTemp2nd;
	}
	
	int nTemp3rd = [RandomHelper getRandomNum:minNumber to:maxNumber];
	if(nTemp3rd != n1st && nTemp3rd != n2nd)
	{
		n3rd = nTemp3rd;
	}
	while(nTemp3rd == n1st || nTemp3rd == n2nd)
	{
		nTemp3rd = [RandomHelper getRandomNum:minNumber to:maxNumber];
		n3rd = nTemp3rd;
	}
	
	int nTemp4th = [RandomHelper getRandomNum:minNumber to:maxNumber];
	if(nTemp4th != n1st && nTemp4th != n2nd && nTemp4th != n3rd)
	{
		n4th = nTemp4th;
	}
	while (nTemp4th == n1st || nTemp4th == n2nd || nTemp4th == n3rd)
	{
		nTemp4th = [RandomHelper getRandomNum:minNumber to:maxNumber];
		n4th = nTemp4th;
	}
	
	int nTemp5th = [RandomHelper getRandomNum:minNumber to:maxNumber];
	if(nTemp5th != n1st && nTemp5th != n2nd && nTemp5th != n3rd && nTemp5th != n4th)
	{
		n5th = nTemp5th;
	}
	while (nTemp5th == n1st || nTemp5th == n2nd || nTemp5th == n3rd || nTemp5th == n4th)
	{
		nTemp5th = [RandomHelper getRandomNum:minNumber to:maxNumber];
		n5th = nTemp5th;
	}
		
	NSNumber* num_1st = [NSNumber numberWithInt:n1st];
	NSNumber* num_2nd = [NSNumber numberWithInt:n2nd];
	NSNumber* num_3rd = [NSNumber numberWithInt:n3rd];
	NSNumber* num_4th = [NSNumber numberWithInt:n4th];
	NSNumber* num_5th = [NSNumber numberWithInt:n5th];
	
	NSArray* numArray = [NSArray arrayWithObjects:num_1st, num_2nd, num_3rd, num_4th, num_5th, nil];
	
	return numArray;
}

+(NSArray*)getRandomArray:(int)minNumber max:(int)maxNumber need:(int)nCount
{
	if(nCount > (maxNumber - minNumber))
	{
		return nil;
	}
	
	NSMutableArray* arrayOfRandom = [NSMutableArray array];
	
	while([arrayOfRandom count] < nCount)
	{
		int nRandomNumber = [RandomHelper getRandomNum:minNumber to:maxNumber];
		
		NSNumber* numNumber = [NSNumber numberWithInt:nRandomNumber];
		
		if(![arrayOfRandom containsObject:numNumber])
		{
			[arrayOfRandom addObject:numNumber];
		}
	}
	return arrayOfRandom;
}


- (void) dealloc
{
	[super dealloc];
}

@end
